class Filter_helper():
    current_processing_arr = []

    def __init__(self):
        self.pre_p_t = {
            "index": -1,
            "type": ''
        }

    def map_raw_data_arr(self, arr):
        raw_arr = [item['data_filter'] for item in arr]
        return raw_arr

    def check_suitable_p_or_t(self, index, data_arr):
        pre = data_arr[index - 1]
        next = data_arr[index + 1]
        current = data_arr[index]
        ans = ''
        if current >= pre and current >= next:
            suitable = self.check_range_more_suitable(index, 'P', current, data_arr)
            if suitable:
                ans = 'P'
        elif current <= pre and current < next:
            suitable = self.check_range_more_suitable(index, 'Z', current, data_arr)
            if suitable:
                ans = 'Z'
        return ans

    def check_range_more_suitable(self, index, type, current, data_arr):
        find_len = 20
        suitable = True
        left = 0 if index - find_len < 0 else index - find_len
        right = len(data_arr) - 1 if index + find_len >= len(data_arr) - 1 else index + find_len
        for i in range(left, right):
            loop_item = data_arr[i]
            if i == index:
                continue
            if type == 'P' and current < loop_item:
                suitable = False
                break
            elif type == 'Z' and current > loop_item:
                suitable = False
                break
        if abs(self.pre_p_t['index'] - index) < 50:
            suitable = False
        elif self.pre_p_t['type'] == type:
            suitable = False
        if suitable is True:
            self.pre_p_t = {
                "index": index,
                "type": type
            }
        return suitable

    @staticmethod
    def smooth_filter_raw_data(arr):
        window_width = 5
        current_processing_arr = Filter_helper.current_processing_arr
        if len(current_processing_arr) + len(arr) < window_width:
            current_processing_arr = current_processing_arr + arr
            return arr
        else:
            smooth_arr = []
            pre_total = sum(current_processing_arr)
            for each in arr:
                current_processing_arr.append(each)
                pre_total = pre_total - current_processing_arr[0] + each
                smooth_data = int(pre_total / (len(current_processing_arr) - 1))
                smooth_arr.append(smooth_data)
                current_processing_arr = current_processing_arr[1:]
            return smooth_arr
